<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-row>
        <el-form-item label="用户搜索">
          <el-input v-model="dataForm.memberMobile" placeholder="用户电话" clearable></el-input>
        </el-form-item>
        <el-form-item label="收银时间">
          <el-date-picker
            v-model="dataForm.time"
            ref="time"
            type="daterange"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :editable="false"
            :picker-options="timeOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="搜索店铺">
          <el-input v-model="dataForm.shopName" placeholder="店铺名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="开单员">
          <el-input v-model="dataForm.kaidanUser" placeholder="姓名/电话" clearable></el-input>
        </el-form-item>
        <el-form-item label="员工筛选">
          <el-input v-model="dataForm.operator" placeholder="员工姓名/电话" clearable></el-input>
        </el-form-item>
        <el-form-item label="订单号搜索">
          <el-input v-model="dataForm.sn" placeholder="订单号" clearable></el-input>
        </el-form-item>
        <el-form-item label="顾客类型">
          <el-select
            v-model="dataForm.vipCardType"
            clearable
            placeholder="全部"
            style="width: 100px;"
          >
            <el-option label="次卡" :value="1"></el-option>
            <el-option label="充值卡" :value="2"></el-option>
            <el-option label="积分卡" :value="3"></el-option>
            <el-option label="散客" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员卡号">
          <el-input v-model="dataForm.vipCardNo" placeholder="会员卡号" clearable></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-row>
      <el-row style="border-top: 2px solid #eee;">
        <el-col :span="8">
          <h2>订单总金额：{{ orderAmount }}</h2>
        </el-col>
        <el-col :span="8">
          <h2>实收金额：{{ payAmount }}</h2>
        </el-col>
        <el-col :span="8">
          <h2>订单笔数：{{ orderNum }}</h2>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      fit
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column prop="sn" header-align="center" align="center" label="订单号" width="170"></el-table-column>
      <el-table-column header-align="center" align="center" label="收银时间" width="135">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.createTime,'YYYY/MM/DD HH:mm') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="memberName" header-align="center" align="center" label="会员姓名"></el-table-column>
      <el-table-column prop="memberMobile" header-align="center" align="center" label="会员电话"></el-table-column>
      <el-table-column prop="orderAmount" header-align="center" align="center" label="订单金额" width="100"></el-table-column>
      <el-table-column prop="payAmount" header-align="center" align="center" label="实收金额" width="100"></el-table-column>
      <el-table-column prop="vipCardNo" header-align="center" align="center" label="会员卡号"></el-table-column>
      <el-table-column prop="vipCardTypeStr" header-align="center" align="center" label="会员卡类型" width="100"></el-table-column>
      <el-table-column prop="shopName" header-align="center" align="center" label="收银店铺"></el-table-column>
      <el-table-column header-align="center" align="center" label="开单员">
        <template slot-scope="scope">
          <span v-if="scope.row.kaidanUser">{{ scope.row.kaidanUser.name + '-' + scope.row.kaidanUser.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="operator" header-align="center" align="center" label="操作员"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="detail(scope.row.id)">详情</el-button>
          <!-- <el-button v-if="isAuth('cash:order:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 详情弹窗 -->
    <OrderDetail v-if="detailVisible" ref="detail" :orderId="orderId" @close="close"></OrderDetail>
  </div>
</template>

<script>
import OrderDetail from "./order-detail";
export default {
  data() {
    return {
      detailVisible: false,
      orderId: null,
      dataForm: {
        memberMobile: "",
        time: [],
        shopName: "",
        operator: "",
        sn: "",
        vipCardType: null,
        vipCardNo: "",
        kaidanUser: ""
      },
      selectDate: "",
      timeOptions: {
        // 大于当前时间不可选
        // disabledDate(time) {
        //   return (
        //     time.getTime() > new Date(new Date().toLocaleDateString()).getTime()
        //   );
        // }
        onPick: ({ maxDate, minDate }) => {
          this.selectDate = minDate.getTime();
          if (maxDate) {
            this.selectDate = "";
          }
        },
        disabledDate: time => {
          if (this.selectDate !== "") {
            const one = 31 * 24 * 3600 * 1000;
            const minTime = this.selectDate - one;
            const maxTime = this.selectDate + one;
            return time.getTime() < minTime || time.getTime() > maxTime;
          }
        }
      },
      orderAmount: 0,
      payAmount: 0,
      orderNum: 0,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    OrderDetail
  },
  activated() {
    this.init();
    this.getDataList();
  },
  methods: {
    init() {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 31);
      this.dataForm.time.push(
        this.dateFormat(start, "YYYY-MM-DD"),
        this.dateFormat(end, "YYYY-MM-DD")
      );
      this.selectDate = end.getTime;
    },
    detail(orderId) {
      this.orderId = orderId;
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.detail.init();
      });
    },
    close() {
      this.detailVisible = false;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      var time = "";
      if (this.dataForm.time && this.dataForm.time.length == 2) {
        time = this.dataForm.time[0] + "~" + this.dataForm.time[1];
      }
      this.$http({
        url: this.$http.adornUrl("/cash/order/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          memberMobile: this.dataForm.memberMobile,
          time: time,
          shopName: this.dataForm.shopName,
          kaidanUser: this.dataForm.kaidanUser,
          operator: this.dataForm.operator,
          sn: this.dataForm.sn,
          vipCardType: this.dataForm.vipCardType,
          vipCardNo: this.dataForm.vipCardNo
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
      this.getData();
    },
    // 计算显示数据
    getData() {
      var time = "";
      if (this.dataForm.time && this.dataForm.time.length == 2) {
        time = this.dataForm.time[0] + "~" + this.dataForm.time[1];
      }
      this.$http({
        url: this.$http.adornUrl("/cash/order/getData"),
        method: "get",
        params: this.$http.adornParams({
          memberMobile: this.dataForm.memberMobile,
          time: time,
          shopName: this.dataForm.shopName,
          operator: this.dataForm.operator,
          sn: this.dataForm.sn,
          vipCardType: this.dataForm.vipCardType,
          vipCardNo: this.dataForm.vipCardNo
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.orderAmount = data.data.orderAmount;
          this.payAmount = data.data.payAmount;
          this.orderNum = data.data.orderNum;
        } else {
          this.orderAmount = 0;
          this.payAmount = 0;
          this.orderNum = 0;
        }
      });
    },
    // 导出excel数据
    exportExcel() {
      if (
        this.dataForm.time == null ||
        typeof this.dataForm.time == undefined ||
        this.dataForm.time.length == 0
      ) {
        this.$message.error("请选择收银时间范围");
        return;
      }
      let time = this.dataForm.time[0] + "~" + this.dataForm.time[1];
      let url = this.$http.adornUrl(`/cash/order/export?token=${this.$cookie.get('token')}`)
      url += `&memberMobile=${this.dataForm.memberMobile}&time=${time}&shopName=${this.dataForm.shopName}` + 
             `&operator=${this.dataForm.operator}&sn=${this.sn}&vipCardType=${this.dataForm.vipCardType}&vipCardNo=${this.vipCardNo}`
      window.open(url, '_blank');
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm("真的要删除所选项么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/cash/order/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
